#!/bin/bash
#author:guoyihot@outlook.com bonainfo.com
#date:2017-09-06
#license:LGPL V3
#description:部署odoo 11.0 shell ubuntu >=14
echo '===================Update Syatem================== '
#description:部署odoo 11.0 shell ubuntu >=14


odoo_options='-c /usr/local/odoo/odoo.config'
odoo_parent_path=/usr/local 
odoo_path=/usr/local/odoo
svc_odoo_user_name=odoo
svc_nginx_user_name=nginx
odoo_config_path=$odoo_path/odoo.config
odoo_pwd=Bonainfo.com
odoo_svc_root=/usr/lib/systemd/system
odoo_svc_path=$odoo_svc_root/odoo.service

sudo apt-get remove unity-webapps-common

echo '===================install wkhtmltopdf================== '
# 安装htmltopdf
# wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.1/wkhtmltox-0.12.1_linux-trusty-amd64.deb
sudo dpkg -i wkhtmltox-0.12.1_linux-trusty-amd64.deb
sudo cp /usr/local/bin/wkhtmltopdf /usr/bin/wkhtmltopdf
sudo chown root:root /usr/bin/wkhtmltopdf
sudo chmod +x /usr/bin/wkhtmltopdf

sudo apt-get update
pip install --upgrade pip
pip install Pypdf2


echo '===================create user odoo for odoo serivce================== '
sudo useradd  $svc_odoo_user_name
sudo passwd $svc_odoo_user_name

echo '===================create user nginx for nginx========================'
sudo useradd $svc_nginx_user_name
sudo passwd $svc_nginx_user_name


echo '===================install soft gcc,git,nginx.......==================='

#for install pgsql 9.6
printf 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo  apt-get update

sudo apt-get install gcc git nginx postgresql-9.6 node.js python3 python3-pip  python-dev libxml2-dev libxslt-dev zlib1g-dev libldap2-dev libsasl2-dev

echo '===================git check out odoo source code .....================='
sudo rm -rf  $odoo_path
sudo mkdir $odoo_path
sudo git clone https://github.com/odoo/odoo.git $odoo_path 
cd $odoo_path
sudo git checkout master
sudo git branch -l 

mkdir idmcoo
chmod -R o+w /usr/local/odoo/idmcoo
cd /root

echo '=================== pip3 install -r requirements.txt====================='
sudo  pip3 install -r  $odoo_path/requirements.txt

sudo apt-get install -y npm
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo npm install -g less

echo '====================config postgresql user info.....==================='
sudo su - postgres psql -c 'createuser -s -P -l -e odoo'

echo  '==================create odoo.config=================================='

#修改文件夹权
sudo chmod o+rw /var/lib
sudo chmod o+rw /var/log

cat<<EOF | sudo tee  $odoo_config_path
[options]
addons_path = /usr/local/odoo/odoo/addons,/usr/local/odoo/addons,/usr/local/odoo/idmcoo
admin_passwd = admin
csv_internal_sep = ,
data_dir = /var/lib/odoo
db_host = False
db_maxconn = 64
db_name = False
db_password = $odoo_pwd
db_port = False
db_template = template1
db_user = $svc_odoo_user_name
dbfilter = .*
demo = {}
email_from = False
geoip_database = /usr/share/GeoIP/GeoLiteCity.dat
import_partial =
limit_memory_hard = 2684354560
limit_memory_soft = 2147483648
limit_request = 8192
limit_time_cpu = 60
limit_time_real = 120
limit_time_real_cron = -1
list_db = True
log_db = False
log_db_level = warning
log_handler = :INFO
log_level = info
logfile = /var/log/odoo/log.txt
logrotate = True
longpolling_port = 8072
max_cron_threads = 2
osv_memory_age_limit = 1.0
osv_memory_count_limit = False
pg_path = None
pidfile = None
proxy_mode = False
reportgz = False
server_wide_modules = web,web_kanban
smtp_password = False
smtp_port = 25
smtp_server = localhost
smtp_ssl = False
smtp_user = False
syslog = False
test_commit = False
test_enable = False
test_file = False
test_report_directory = False
translate_modules = ['all']
unaccent = False
without_demo = False
workers = 0
xmlrpc = True
xmlrpc_interface =
xmlrpc_port = 8069
EOF

#配置odoo服务
echo  '===========================config odoo.service======================='
sudo mkdir $odoo_svc_root

cat<<EOF | sudo tee $odoo_svc_path
[Unit]

Description=Odoo

After=postgresql.service

[Service]

Type=simple

User=odoo

Group=odoo

ExecStart=/usr/local/odoo/odoo-bin $odoo_options

[Install]

WantedBy=multi-user.target

EOF

echo '================================enble odoo.service===================='
sudo systemctl daemon-reload
sudo systemctl enable $odoo_svc_path
echo '================================restart odoo.service=================='
sudo systemctl restart odoo.service


#配置nginx
#先备份nginx
echo '===============================config nginx=========================='
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx_conf_default

http_host='$http_host'
remote_addr='$remote_addr'
scheme='$scheme'
http_upgrade='$http_upgrade'

cat<<EOF | sudo tee /etc/nginx/nginx.conf
 worker_processes 2;
 events {
 use epoll;
 worker_connections 65535;
 }
 http {
 include mime.types;
 default_type application/octet-stream;
 sendfile on;
 server_tokens off;
 keepalive_timeout 200;
 tcp_nodelay on;
 gzip on;
 upstream fkcloud {
 server 127.0.0.1:8069;
 }
 server {
 listen 80;
 server_name localhost;
 location / {
 proxy_pass_header Server;
 proxy_set_header Host $http_host;
 proxy_redirect off;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Scheme $scheme;
 proxy_http_version 1.1;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection "Upgrade";
 proxy_pass http://fkcloud;
 client_max_body_size 40m;
 }
 }
 }


EOF

sudo systemctl daemon-reload

#备份 sites-available
printf cp /etc/nginx/sites-available/default  /home/$svc_nginx_user_name/sites-available_default
sudo rm -rf  /etc/nginx/sites-available/default

# 解决Failed to read PID from file /run/nginx.pid: Invalid argument
sudo mkdir -p /etc/systemd/system/nginx.service.d 
echo "[Service]\nExecStartPost=/bin/sleep 0.1\n" | sudo tee /etc/systemd/system/nginx.service.d/override.conf
sudo systemctl daemon-reload
sudo nginx -s reload
sudo systemctl restart nginx.service

